﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Adams.Shared;

namespace Adams.Web.Logic.Security
{
    public static class Captcha
    {
        public static string GenerateCaptcha(string randomText)
        {
            string code = Shared.Crypt.Encrypt(randomText + "|" + DateTime.UtcNow.ToString());
            return code;
        }

        public static bool ValidateCaptcha(string input, string code)
        {
            string en = Shared.Crypt.Decrypt(code);
            string[] items = en.Split('|');
            var text = items[0];
            DateTime d = DateTime.Parse(items[1]);

            if (Shared.Str.IsEqual(text, input))
            {
                if (Shared.DateHelper.DiffMins(d, DateTime.UtcNow) < 420)
                {
                    return true;
                }
            }

            return false;
        }
    }
}
